﻿var CustomGrid_Selecteds = [];
var	thereArePagers = false;

// Mozilla: Hay que setear el tam del tbody para que el header quede fijo
function CustomGrid_FixHeader(idtable)
{
    if(Common.isFirefox()) {
	    var table = document.getElementById(idtable);
    	var container = table.offsetParent;
		var thead = table.tHead;
	    var tbody = table.tBodies[0];
        
    	if(table.offsetHeight>container.offsetHeight) {
            tbody.style.height = (container.offsetHeight - thead.offsetHeight - 22).toString() + "px";
        }
	}
}

function CustomGrid_Refresh(gridID)
{
    CustomGrid_ClearSelection(gridID);
    CustomGrid.Refresh(gridID, CustomGrid_RefreshCallBack, gridID);
}

function CustomGrid_LiteRefresh(gridID)
{
    CustomGrid.RefreshSamePage(gridID, CustomGrid_RefreshCallBack, gridID);
}

function CustomGrid_RefreshCallBack(response)
{
    var gridID = response.context;
    
    document.body.style.cursor = "default";
    
    if(response.value) 
    {
        var pageNumber = response.value[0];
        var lastPage = response.value[1];
        var list = response.value[2];
        
        var container = document.getElementById(gridID);
        container.innerHTML = list;
        
        // Pager Refresh
        if(thereArePagers) {
            var pagerId = Pager_References[gridID];
            
            if(pagerId) {
                Pager_Instances[pagerId].pageNumber = parseInt(pageNumber, 10);
                Pager_Instances[pagerId].lastPage = parseInt(lastPage, 10);
                Pager_Instances[pagerId].refresh();
            }
        }
                
        // Selecteds Refresh
        CustomGrid_RefreshSelecteds(gridID);
    }
    else if (response.error)
    {
        alert(response.error);
    }
    else
    {
        alert("Unknow Error.");
    }
}

function CustomGrid_CleanFiltersCallBack(response)
{
    CustomGrid_RefreshCallBack(response);

    if( (typeof(ConfigManager) !== "undefined") && (ConfigManager.idGrid != "") )
        ConfigManager.refresh();    
}

function CustomGrid_GetSelecteds(gridID) 
{
    if (CustomGrid_Selecteds[gridID] == null) {
        CustomGrid_Selecteds[gridID] = [];
    }

    return CustomGrid_Selecteds[gridID];
}

function CustomGrid_RefreshSelecteds(gridID)
{
    var selecteds = CustomGrid_GetSelecteds(gridID);

    if(selecteds) {
        var chks = $("input[name=\"checkbox_" + gridID + "\"]");
        
        if(chks) {
            
            // chks comtiene un array de checkboxes
            if(chks.length) {
                for (var i = 0; i < chks.length; i++) {
                    if (selecteds[chks[i].value]) {
                        chks[i].checked = true;
                        
                        var row;
                        if(chks[i].parentElement)
                            row = chks[i].parentElement.parentElement;
                        else
                            row = chks[i].parentNode.parentNode;

                        row.setAttribute("originalClass", row.className);
                        row.className = "checkedRow";
                    }                         
                }
            }

            // si hay un solo item en la pagina, chks es el checkbox correspondiente a ese item 
            // y no un array de checkboxes
            else {
                chks.checked = selecteds[chks.value];
                
                var row;
                if(chks.parentElement)
                    row = chks.parentElement.parentElement;
                else
                    row = chks.parentNode.parentNode;
                                            
                row.setAttribute("originalClass", row.className);
                row.className = "checkedRow";                
            }
        }
    }
}

function CustomGrid_ClearSelection(gridID)
{
    CustomGrid_Selecteds[gridID] = [];
}

function CustomGrid_GetSelection(gridID)
{
    var selecteds = CustomGrid_GetSelecteds(gridID);  
    var ret = [];
    
    for (var id in selecteds) {
        if(selecteds[id])
	        ret.push(id);
    }    
    
    return ret;
}

function CustomGrid_SelectByValue(gridID, value) {
    var selecteds = CustomGrid_GetSelecteds(gridID);
    selecteds[value] = true;
}

function CustomGrid_SelectAll(gridID, value)
{
    var chks = $("input[name=\"checkbox_" + gridID + "\"]");
    if(!chks)
        return;
        
    // chks comtiene un array de checkboxes
    if(chks.length) {
        for (var i = 0; i < chks.length; i++) {
            chks[i].checked = value;
            CustomGrid_Select(gridID, chks[i]);
        }
    }

    // si hay un solo item en la pagina, chks es el checkbox correspondiente a ese item 
    // y no un array de checkboxes
    else {
        chks.checked = value;
        CustomGrid_Select(gridID, chks);                
    }    
}

function CustomGrid_Select(gridID, check)
{
    var row;
    
    if(check.parentElement)
        row = check.parentElement.parentElement;
    else
        row = check.parentNode.parentNode;
    
    var rowID = check.value;
    var value = check.checked;
    
    if(rowID=="")
        return;
        
    var selecteds = CustomGrid_GetSelecteds(gridID);
    selecteds[rowID] = value;
    
    if(!value) {
        var chkall = $("input[name=\"checkboxall_" + gridID + "\"]");
        chkall.checked = false;
    }
    
    // Styles
    if(value) {
        if( (!row.getAttribute("originalClass")) || (row.getAttribute("originalClass") == "") )
            row.setAttribute("originalClass", row.className);
            
        row.className = "checkedRow";
        row.style.backgroundColor = "";   
    }
    else {
        row.className = row.getAttribute("originalClass");
        row.style.backgroundColor = "";      
    }
}

function CustomGrid_RollOver(row)
{
    var bgColor = Common.getBgColor(row);
    bgColor.red -= 10;
    bgColor.green -= 10;
    bgColor.blue -= 10;
    
    Common.setBgColor(row, bgColor)
}

function CustomGrid_RollOut(row)
{
    row.style.backgroundColor = "";
}

function CustomGrid_OrderBy(gridID, field) {
    CustomGrid.OrderBy(gridID, field, CustomGrid_OrderByCallBack, gridID);
}

function CustomGrid_OrderByCallBack(response) {
    CustomGrid_RefreshCallBack(response);

    if ((typeof (ConfigManager) !== "undefined") && (ConfigManager.idGrid != ""))
        ConfigManager.refresh();
}